#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include "serial.h"
#include "server.h"
#include "mypthread.h"

pthread_t cam_tid;
pthread_t zgb_tid;
pthread_t cli_tid;

int main(int argc, char *argv[])
{   
    pthread_mutex_t cam_mutex;
    int cli_ret = -1 , cam_ret = -1, zgb_ret = -1;
    pthread_mutex_init(&cam_mutex, NULL);
    while(cam_ret < 0)
    {
        cam_ret = pthread_create(&cam_tid, NULL, pthread_cam, NULL);
        if (cam_ret) 
        {
            perror("camera pthread_create");
            exit(1);
        }
        else
            printf("camera pthread_create success\n");
        cam_ret = pthread_detach(cam_tid);
        if (cam_ret) 
        {
            perror("camera pthread_detach");
            exit(1);
        }
        else
            printf("camera pthread_detach success\n");
    }
    while(zgb_ret < 0)
    {
        zgb_ret = pthread_create(&zgb_tid, NULL, pthread_zgb, NULL);
        if (zgb_ret) 
        {
            perror("zigbee pthread_create");
            exit(1);
        } 
        else
            printf("zigbee pthread_create success\n");
        zgb_ret = pthread_detach(zgb_tid);
        if (zgb_ret) 
        {
            perror("zigbee pthread_detach");
            exit(1);
        } 
        else
            printf("zigbee pthread_detach success\n");
    }
    while (cli_ret < 0)
    {
        cli_ret = pthread_create(&cli_tid, NULL, pthread_cli, NULL);
        if (cli_ret) 
        {
            perror("cli pthread_create\n");
            exit(1);
        } 
        else
            printf("cli pthread_create success\n");
        cli_ret = pthread_detach(cli_tid);
        if (cli_ret) 
        {
            perror("cli pthread_detach\n");
            exit(1);
        }
        else
            printf("cli pthread_detach success\n");
    }
    while (1);
    exit(0);
    return 0;
} 
